const obj = {
  title: 'hello',
  age: 18,
  _name: '_name',
  get name() {
    console.log('this', this); // 默认是 obj 对象
    return this._name;
  },
  set name(newValue) {
    this._name = newValue;
  },
};

const objProxy = new Proxy(obj, {
  get(target, key, receiver) {
    // *把 this 绑定为 objProxy ,这样 obj 中的 get 和 set 方法中的更改就可以进行监听
    return Reflect.get(target, key, receiver);
  },
  set(target, key, newValue, receiver) {
    const isSuccess = Reflect.set(target, key, newValue, receiver);
    if (!isSuccess) throw new Error('设置失败');
  },
});

console.log(objProxy.name);
